// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// No Node.js APIs are available in this process because
// `nodeIntegration` is turned off. Use `preload.js` to
// selectively enable features needed in the rendering
// process.
const moment = require('moment')
const { MessageHandler } = require('./message.js')

moment.locale('zh-cn')

let msgHandler = new MessageHandler()

// ----------------------------------------
// 注册监听器
// ----------------------------------------

// 电子秤
msgHandler.on('weight/data', (topic, message) => {
	const msg = JSON.parse(message)
	document.querySelector('#weight').innerHTML = msg.data.toFixed(2)
	let state = false
	if (msg.state == "ST") {
		state = true
	}
	document.querySelector('#state').innerHTML = state ? '稳定':'不稳定'
})

// RFID
msgHandler.on('rfid/data', (topic, message) => {
	const msg = JSON.parse(message)
	document.querySelector('#rfid').innerHTML = msg.epc
})

let btnClose = document.getElementById("close")
btnClose.onclick = function() {
	window.close()
}

let timeHandler = function() {
	const now = moment().format('MMM Do YY dddd')
	document.querySelector('#now').innerHTML = now
}

setInterval(timeHandler, 1000)

